<?php
/**
 * 临时工程
 * @author cmd
 * @since 29-06-2011
 */
class foodApp extends app
{
	public function index(){

		$post = $this->getData('post');

		$foodmod = $this->getModel('food');
		//是否登录

		//默认的页面,一级搜索
		$list = array();
		$tips = $foodmod->get_tip_by_city('beijing');
		$top_menu = $foodmod->menu_get_top_one('beijing');
		$comments = $foodmod->comment_get_new(5);//显示5条最新评论
		if(!empty($post['keyword'])){
			$list = $foodmod->menu_search_by_keyword($post['keyword'],10,1);
		}else{
			if(intval(Cookie::get('food_user_id')) == 0){
				if(empty($post)){
					$list = $foodmod->menu_search_1('beijing', 9, 1);
				}else{
					is_numeric($post['age']) || $post['age']=0;
					is_numeric($post['sex']) || $post['sex']=0;
					$post['sex'] = intval($post['sex']);
					$post['age'] = intval($post['age']);
					$list = $foodmod->menu_search_2($post['sex'],$post['age'],'beijing',9,1);
				}
			}else{
				$uid = intval(Cookie::get('food_user_id'));
				$user = $foodmod->user_get_by_id($uid);
				if(!empty($user)){
					$list = $foodmod->menu_search_3($uid, 4,1);
				}
			}
		}
		//排行榜
		$ranks = $foodmod->menu_get_by_score(5);
		return array('comments' => $comments ,'lists' => $list ,'tips' => $tips, 'u' => $user,'top'=>$top_menu,'curdate'=>date('Y/m/d'),'ldate'=>$foodmod->get_ldate(),'city'=>'北京','ranks'=>$ranks);
	}

	public function personpage(){
		return array();
	}

	public function register(){

		$post = $this->getData('post');

		if(empty($post)){
			return array();
		}
		$foodmod = $this->getModel('food');

		if($post['user_name'] == ''){
			//$this->inner_msg('请输入名字');
			return array('msg' => '请输入名字');
		}
		if($foodmod->user_get_count_by_user_name($post['user_name']) > 0){
			return array('msg' => '该用户名已经被注册');
		}
		if($post['password'] == ''){
			//$this->inner_msg('请输入密码');
			return array('msg' => '请输入密码');
		}
		if($post['confirmpassword'] == ''){
			//$this->inner_msg('请输入确认密码');
			return array('msg' => '请输入确认密码');
		}
		if($post['password'] != $post['confirmpassword']){
			//$this->inner_msg('密码不一致');
			return array('msg' => '密码不一致');
		}


		$foodmod->user_add($post['user_name'],$post['password']);
		go_to('/food/set');

	}


	public function logout(){
		//$id = intval(Cookie::get('food_user_id'));
		$foodmod = $this->getModel('food');
		$foodmod->user_logout();
		go_to('/food/index');
	}

	public function login(){
		$data = $this->getData('post');
		if(empty($data)){
			return array();
		}
		$foodmod = & $this->getModel('food');
		$r = $foodmod->user_login($data['username'], $data['password']);
		if($r > 0){
			go_to('/food/index');
		}else{
			switch ($r){
				case -2 : return array('msg' => '该用户不存在！');//$this->inner_msg('该用户不存在!');
				case -3 : return array('msg' => '用户密码错误！');//$this->inner_msg('用户密码错误！');
			}
		}

	}


	public function addcomment(){
		$data = $this->getData('post');
		$foodmod = $this->getModel('food');
		if(empty($data)){
			return array();
		}
		if(intval(Cookie::get('food_user_id')) == 0){
			$foodmod->message_out('尚未登录，请先登录后再评论');
			go_to('/food/foodcontent?menu_id='.$data['menu_id']);
			//return array('msg' => '尚未登录，请先登录后再评论');
		}
		if($data['message'] == ''){
			$foodmod->message_out('请输入评论');
			go_to('/food/foodcontent?menu_id='.$data['menu_id']);
			//return array('msg' => '请输入评论');
		}
		$foodmod->comment_add(intval(Cookie::get('food_user_id')),$data['menu_id'],$data['message'],$data['score']);
		go_to('/food/foodcontent?menu_id='.$data['menu_id']);

	}

	public function foodcontent(){
		$menu_id = $this->getData('get/menu_id');
		if($menu_id){
			$foodmod = $this->getModel('food');
			$menu = $foodmod->menu_get_by_id($menu_id);
			$comments = $foodmod->comment_get($menu_id,30,1);

		}
		return array('menu' => $menu, 'comments' => $comments);
	}
	public function set(){
		$post = $this->getData('post');
		$foodmod = $this->getModel('food');
		$uid = intval(Cookie::get('food_user_id'));
		//$user = $foodmod->get
		if(empty($post)){
			return array();
		}
		if($post['city'] == ''){
			//$this->inner_msg('请输入城市');
			return array('msg' => '请输入城市');
		}
		if($post['sex'] == '0'){
			//$this->inner_msg('请选择性别');
			return array('msg' => '请选择性别');
		}
		if($post['age'] == ''){
			//$this->inner_msg('请输入年龄');
			return array('msg' => '请输入年龄');
		}
		if(!is_numeric($post['sex'])){
			//$this->inner_msg('性别是非法数据');
			return array('msg' => '性别是非法数据');
		}
		if(!is_numeric($post['age'])){
			//$this->inner_msg('年龄是非法数据');
			return array('msg' => '年龄是非法数据');
		}
		$post['age'] = intval($post['age']);
		$post['sex'] = intval($post['sex']);


		$foodmod->user_modify($uid,$post['city'],$post['sex'],$post['age'],170,60);

		go_to('/food/personpage');


		return array();
	}

	public function test(){
		return array();
	}

	public function index_v(){
		$foodmod = $this->getModel('food');
		$list = $foodmod->menu_search_1('beijing', 9, 1);
		//print_r($list);
		//exit;
		return array('lists' => $list, 'curdate'=>date('Y/m/d'),'ldate'=>$foodmod->get_ldate(),'city'=>'北京', 'day' => '炎热');

	}

	public function foodcontent_v(){
		$menu_id = $this->getData('get/menu_id');
		if($menu_id){
			$foodmod = $this->getModel('food');
			$menu = $foodmod->menu_get_by_id($menu_id);
		}
		return array();
		return array('menu' => $menu);
	}

	public function searchresult_v(){
		return array();
	}

}
?>